<template>
  <view class="index">
    <view class="modes">
      <button size="mini" @click="onMode('recite')">
        背题模式
      </button>
      <button size="mini" @click="onMode('practice')">
        刷题模式
      </button>
      <button size="mini" @click="onMode('exam')">
        模拟考试
      </button>
    </view>
    <view class="libs">
      <text>当前题库：{{ curLib }}</text>
      <text style="color: #66ccff; margin-left: 24px" @click="changeLib">点击切换</text>
    </view>
    <view class="simple-btn">
      <button size="mini" @click="toSimple">
        简单版
      </button>
    </view>
  </view>
</template>

<script>
import tempdata from '../../temp-data.js'
import question from "@/api/question";
export default {
  data() {
    return {
      curLib: 'mh_secur_4',
      libs: ['mh_secur_4', 'mh_secur_5']
    }
  },
  methods: {
    onMode(type) {
      uni.navigateTo({
        // url: '/uni_modules/sv-exam-plus/pages/exam/exam',
		url:'/pages/exam/exam',
        success: async (res) => {
          res.eventChannel.emit('e-transmit-exams', {
            param: {
              from_lib: this.curLib,
              unit: 'unit-1'
            },
            opt: {
              lib_name: this.curLib,
              type: type
            },
            apifunc: {
              examList: this.getExamList
            }
          })
        }
      })
    },
    changeLib() {
      uni.showActionSheet({
        itemList: this.libs,
        success: (res) => {
          this.curLib = this.libs[res.tapIndex]
        }
      })
    },
    toSimple() {
      uni.navigateTo({
        url: '/pages/simple/simple',
        success: async (res) => {
          const examDataRes = await this.getExamList({ from_lib: this.curLib, unit: 'unit-1' })
          res.eventChannel.emit('e-transmit-exams', {
            data: examDataRes.data
          })
        }
      })
    },
    /**
     * ajax请求 此处仅简化代码 不做封装 封装方式请移步以下链接
     * @tutorial https://ext.dcloud.net.cn/plugin?id=16529
     */
	 async getExamListold(params = {}) {
	   let data = await question.getQuestionList({});
	   console.log("data", data); // 现在这里才是实际数据
	   console.log("data.list", data && data.list);
	   tempdata.data= data.list
	   return tempdata;
	 },
	 async getExamList(params = {}) {
       return tempdata;
	   let data = await question.getQuestionList(params);
	   console.log("data", data);

	   const from_lib = params.from_lib || '';
	   const unit = params.unit || '';
	   const examList = (data.list || []).map((q, idx) => this.convertQuestionToExamItem(q, {from_lib, unit, index: idx + 1}));

	   return { data: examList };
	 },
	 convertQuestionToExamItem(q, {from_lib = '', unit = '', index = 1} = {}) {
	   return {
	     _id: q.id ? String(q.id) : '', // 或者用 uuid
	     from_lib,
	     unit,
	     exam_id: `${from_lib}-${unit}-${index}`,
	     exam_name: q.question ? `<p>${q.question}</p>` : '',
	     exam_title: q.question || '',
	     exam_index: index,
	     exam_type: q.type || 0,
	     exam_option: Object.keys(q.option || {}).map(key => ({
	       key,
	       value: `<div>${q.option[key]}</div>`
	     })),
         options_type:q.options_type,
	     exam_answer: q.true? String(q.true).split(',').map(s => s.trim()).filter(Boolean): [],
	     analysis: q.analysis ? `<p>${q.analysis}</p>` : '',
	     comment: q.comment ? `<p>${q.comment}</p>` : '',
	   }
	 }



  }
}
</script>

<style lang="scss">
.index {
  width: 100%;
  height: calc(100vh - var(--window-top));
  padding: 16px 0;
  box-sizing: border-box;

  .modes,
  .simple-btn {
    width: 100%;
    display: flex;
    justify-content: space-between;
  }

  .libs {
    font-size: 14px;
    padding: 16px;
    box-sizing: border-box;
  }

  .simple-btn {
    margin-top: 100rpx;
  }
}
</style>
